package com.fiberhome.ztelauncher.core.screensaver;

import android.app.Activity;
import android.app.KeyguardManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.PowerManager;
import android.view.KeyEvent;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

import com.fiberhome.ztelauncher.R;
import com.fiberhome.ztelauncher.util.BitmapUitl;

import java.util.ArrayList;
import java.util.List;

import static android.content.ContentValues.TAG;

public class ScreenSaveActivity extends Activity {

    private ImageView ivFirst;
    private ImageView ivSecond;
    private List<String> listUrl = new ArrayList<>();
    private int index;
    private int duration = 5000;
    private PowerManager.WakeLock mWakeLock;
    private int transition;
    private final static int FADE_ANIM = 0;
    private final static int PLATE_ANIM = 1;
    private final static int ZOOM_ANIM = 2;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case FADE_ANIM:
                    startFadeAnim();
                    handler.sendEmptyMessageDelayed(FADE_ANIM, duration);
                    break;
                case PLATE_ANIM:
                    startPlateAnim();
                    handler.sendEmptyMessageDelayed(PLATE_ANIM, duration);
                    break;
                case ZOOM_ANIM:
                    startZoomAnim();
                    handler.sendEmptyMessageDelayed(ZOOM_ANIM, duration);
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_screen_save);

        listUrl = getIntent().getStringArrayListExtra("imgUrl");
//        listUrl.add("720p_en/assets/images/Ads_Banner/03a9d016-b2d1-40bc-8b0f-559959cdcd78.jpg");
//        listUrl.add("720p_en/assets/images/Ads_Banner/8aa1804a-d77a-4d28-9d31-da7a1945b3e0.jpg");
//        listUrl.add("720p_en/assets/images/Ads_Banner/194813cb-f64f-4181-b326-a513db415256.jpg");
//        listUrl.add("720p_en/assets/images/Ads_Banner/8a3a89fa-812a-4b14-b8a5-0e0dc909f60c.jpg");
//        listUrl.add("720p_en/assets/images/Ads_Banner/d590bf3c-c4a6-419e-8677-093095e51b48.jpg");
        ivFirst = (ImageView) findViewById(R.id.iv_one);
        ivSecond = (ImageView) findViewById(R.id.iv_two);

        registerReceiver(mMasterResetReciever, new IntentFilter(Intent.ACTION_SCREEN_OFF));
        PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
        mWakeLock = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.SCREEN_DIM_WAKE_LOCK |
                PowerManager.ON_AFTER_RELEASE, "SimpleTimer");

        KeyguardManager mKeyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
        KeyguardManager.KeyguardLock mKeyguardLock = mKeyguardManager.newKeyguardLock(TAG);
        mKeyguardLock.disableKeyguard();

        if (listUrl.size() % 2 != 0) {
            listUrl.addAll(listUrl);
        }
        BitmapUitl.loadBitmap(this, ivFirst, listUrl.get(0));
        BitmapUitl.loadBitmap(this, ivSecond, listUrl.get(1));

        transition = 2;
        checkTransition();
    }

    private void checkTransition(){
        if (transition == 0) {
            handler.sendEmptyMessageDelayed(FADE_ANIM, duration);

        } else if (transition == 1) {
            handler.sendEmptyMessageDelayed(PLATE_ANIM, duration);

        } else if (transition == 2) {
            handler.sendEmptyMessageDelayed(ZOOM_ANIM, duration);

        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        mWakeLock.acquire();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mWakeLock.acquire();
    }

    /**
     * 渐变动画
     */
    private void startFadeAnim() {
        index++;
        index = index % listUrl.size();
        if (index % 2 == 1) {
            BitmapUitl.loadBitmap(getApplicationContext(), ivSecond, listUrl.get(index));
            ivFirst.bringToFront();
            ivSecond.clearAnimation();
            ivFirst.startAnimation(AnimationUtils.loadAnimation(ScreenSaveActivity.this,
                    R.anim.fade_out));

        } else {
            BitmapUitl.loadBitmap(getApplicationContext(), ivFirst, listUrl.get(index));
            ivSecond.bringToFront();
            ivFirst.clearAnimation();
            ivSecond.startAnimation(AnimationUtils.loadAnimation(ScreenSaveActivity.this,
                    R.anim.fade_out));
        }
    }

    /**
     * 胶卷拉动动画
     */
    private void startPlateAnim() {
        index++;
        index = index % listUrl.size();
        String imgUrl = listUrl.get(index);
        if (index % 2 == 1) {
            BitmapUitl.loadBitmap(ScreenSaveActivity.this, ivSecond, imgUrl);
            ivFirst.startAnimation(AnimationUtils.loadAnimation(ScreenSaveActivity.this,
                    R.anim.translate_out_left_screen));
            ivSecond.startAnimation(AnimationUtils.loadAnimation(ScreenSaveActivity.this,
                    R.anim.translate_in_right_screen));
            ivFirst.setVisibility(View.GONE);
            ivSecond.setVisibility(View.VISIBLE);

        } else {
            BitmapUitl.loadBitmap(ScreenSaveActivity.this, ivFirst, imgUrl);
            ivFirst.startAnimation(AnimationUtils.loadAnimation(ScreenSaveActivity.this,
                    R.anim.translate_in_right_screen));
            ivSecond.startAnimation(AnimationUtils.loadAnimation(ScreenSaveActivity.this,
                    R.anim.translate_out_left_screen));
            ivFirst.setVisibility(View.VISIBLE);
            ivSecond.setVisibility(View.GONE);
        }
    }

    /**
     * 缩放动画
     */
    private void startZoomAnim() {
        index++;
        index = index % listUrl.size();
        if (index % 2 == 1) {
            BitmapUitl.loadBitmap(ScreenSaveActivity.this, ivSecond, listUrl.get(index));
            ivFirst.bringToFront();
            ivFirst.startAnimation(AnimationUtils.loadAnimation(ScreenSaveActivity.this,
                    R.anim.scale_small));
            ivSecond.clearAnimation();

        } else {
            BitmapUitl.loadBitmap(ScreenSaveActivity.this, ivFirst, listUrl.get(index));
            ivSecond.bringToFront();
            ivSecond.startAnimation(AnimationUtils.loadAnimation(ScreenSaveActivity.this,
                    R.anim.scale_small));
            ivFirst.clearAnimation();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeMessages(transition);
        unregisterReceiver(mMasterResetReciever);
    }

    BroadcastReceiver mMasterResetReciever = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            try {
                Intent i = new Intent();
                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                i.setClass(context, ScreenSaveActivity.class);
                context.startActivity(i);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };

    /**
     * @param context
     * @param listUrl
     */
    public static void goInto(Context context, ArrayList<String> listUrl) {
        Intent intent = new Intent(context, ScreenSaveActivity.class);
        intent.putStringArrayListExtra("imgUrl", listUrl);
        context.startActivity(intent);
        ((Activity) context).overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        finish();
        return super.dispatchKeyEvent(event);
    }
}
